package org.jsbd.boss.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.SimulatorFlowLog;
import org.jsbd.boss.domian.channel.BaseResultVo;
import org.jsbd.boss.domian.channel.SimulatorActionLog;
import org.jsbd.boss.domian.channel.SimulatorNewActionLog;
import org.jsbd.boss.enums.ResponseEnum;
import org.jsbd.boss.mq.MqLogManager;
import org.jsbd.boss.service.ISimulatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.hziee.common.queue.DelayExecuteBuffer;

@Controller
@RequestMapping("/actionlog")
public class SimulatorActionLogController {
	public static final Logger LOGGER = LoggerFactory.getLogger(SimulatorActionLogController.class);
	@Resource
	private ISimulatorService simulatService;
	
	@Autowired
	private DelayExecuteBuffer<SimulatorNewActionLog> simulatorNewActionLogBuffer;

	@Autowired
	private DelayExecuteBuffer<SimulatorFlowLog> simulatorFlowLogBuffer;
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void addLog(HttpServletResponse response, SimulatorActionLog log) {
		BaseResultVo resultVo = new BaseResultVo();
		try {
			if (log.getStep() == null || StringUtils.isBlank(log.getOrderId()) || StringUtils.isBlank(log.getUuid()) || StringUtils.isBlank(log.getImsi()) || StringUtils.isBlank(log.getMark())) {
				resultVo.setStatus(ResponseEnum.FAIL.getStatus());
				resultVo.setMessage("请求参数非法!");
			} else {
				boolean flag = simulatService.batchInsert(log);
				if (!flag) {
					ResponseUtils.responseFailure(response);
				}
			}
		} catch (Exception e) {
			LOGGER.error("保存行为日志失败", e);
			resultVo.setStatus(ResponseEnum.ERROR.getStatus());
			resultVo.setMessage(e.getMessage());
		}
		ResponseUtils.renderJson(response, resultVo.toString());
	}
	
	@RequestMapping(value = "/saveNew", method = RequestMethod.POST)
	public void saveLog(HttpServletResponse response, SimulatorNewActionLog log) {
		BaseResultVo resultVo = new BaseResultVo();
		MqLogManager.saveNewActionLog(log);
		ResponseUtils.renderJson(response, resultVo.toString());
	}
	
	/**
	 * 
	 * 流量统计
	 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
	 * @param payLogBean
	 * @return
	 */
	@RequestMapping(value = "/flow")
	@ResponseBody
	public Map<String, Object> flow(SimulatorFlowLog flowLog) {
		Integer type  = flowLog.getType();
		if (type == 1) {
			String uuid = flowLog.getUuid();
			String sysVer = flowLog.getSysVer();
		    String mark = flowLog.getMark();
		    Long size = flowLog.getSize();
		    String remark = flowLog.getRemark();
		    Long time = flowLog.getTime();
			
			SimulatorFlowLog fl = new SimulatorFlowLog();
			fl.setUuid(uuid);
			fl.setSysVer(sysVer);
			fl.setMark(mark);
			fl.setSize(Long.valueOf(remark));
			fl.setType(3);
			fl.setTime(time);
			
			flowLog.setRemark("");
			simulatorFlowLogBuffer.add(flowLog);
			simulatorFlowLogBuffer.add(fl);
		} else {
			simulatorFlowLogBuffer.add(flowLog);
		}
		return ResponseMapUtils.responseSuccess();
	}
	
}
